//
//  BMPlayerCustomControlView.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/10.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit


class BMPlayerCustomControlView: BMPlayerControlView {
//    var playTimeUIProgressView:UIProgressView!
//    var fullBottomMaskView:UIView!
    var sendBarrageBtn:UIButton! = UIButton.init(type: UIButtonType.custom) //发送弹幕按钮
    var switchBtn:UIButton! = UIButton.init(type: UIButtonType.custom) //弹幕显示开关按钮
    fileprivate var backgroundView:UIView!
    
    override func customizeUIComponents() {
        chooseDefitionView.isHidden = true
        
        mainMaskView.backgroundColor = UIColor.clear
        topMaskView.backgroundColor    = UIColor.black.withAlphaComponent(0.4)
        
        backgroundView = UIView()
        backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        self.backgroundView.isUserInteractionEnabled = false
        
        self.sendBarrageBtn.backgroundColor = UIColor.lightGray
        self.sendBarrageBtn.titleLabel?.font = UIFont.BoldSysFont(13)
        self.sendBarrageBtn.setTitle("发个弹幕呗", for: UIControlState())
        self.sendBarrageBtn.setTitleColor(UIColor.white, for: UIControlState())
        self.sendBarrageBtn.layer.cornerRadius = 14
        self.sendBarrageBtn.tag = ButtonType.sendbarrage.rawValue
        self.sendBarrageBtn.addTarget(self, action: #selector(self.sendBarrageBtnOnClick(_:)), for: .touchUpInside)
        
        self.switchBtn.backgroundColor = UIColor.clear
        self.switchBtn.setImage(UIImage.init(named: "barrage_unshow"), for: UIControlState.selected)
        self.switchBtn.setImage(UIImage.init(named: "barrage_show"), for: .normal)
        self.switchBtn.tag = ButtonType.showbarrage.rawValue
        self.switchBtn.addTarget(self, action: #selector(self.showBarrageBtnClick(_:)), for: .touchUpInside)
        
    }
    
    @objc fileprivate func sendBarrageBtnOnClick(_ button: UIButton){
        delegate?.controlView(controlView: self, didPressButton: button)
    }
    
    @objc fileprivate func showBarrageBtnClick(_ button: UIButton){
        if button.isSelected{
            button.isSelected = false
            button.tag = ButtonType.showbarrage.rawValue
        }else{
            button.isSelected = true
            button.tag = ButtonType.notshowbarrage.rawValue
        }
        delegate?.controlView(controlView: self, didPressButton: button)
    }
    
    override func updateUI(_ isForFullScreen: Bool) {
        super.updateUI(isForFullScreen)
        self.updateBottomViewConstraint(isForFullScreen)
    }
    
    override func playTimeDidChange(currentTime: TimeInterval, totalTime: TimeInterval) {
        super.playTimeDidChange(currentTime: currentTime, totalTime: totalTime)
    }
    
    override func playStateDidChange(isPlaying: Bool) {
        super.playStateDidChange(isPlaying: isPlaying)
    }

    override func controlViewAnimation(isShow: Bool) {
        super.controlViewAnimation(isShow: isShow)
    }
    
    fileprivate func updateBottomViewConstraint(_ isForFullScreen: Bool){
        if isForFullScreen{
            if self.sendBarrageBtn.superview == nil{
                self.bottomMaskView.addSubview(self.sendBarrageBtn)
                self.bottomMaskView.addSubview(self.switchBtn)
                self.bottomMaskView.addSubview(self.backgroundView)
                self.bottomMaskView.sendSubview(toBack: self.backgroundView)
            }
            
            bottomMaskView.snp.remakeConstraints{ (make) in
                make.bottom.left.right.equalTo(mainMaskView)
                make.height.equalTo(70)
            }
            
            self.backgroundView.snp.remakeConstraints({ (make) in
                make.bottom.left.right.equalTo(mainMaskView)
                make.height.equalTo(50)
            })
            
            currentTimeLabel.snp.remakeConstraints { (make) in
                make.left.equalTo(mainMaskView).offset(10)
                make.top.equalTo(bottomMaskView)
                make.width.equalTo(40)
            }
            
            totalTimeLabel.snp.remakeConstraints { (make) in
                make.centerY.equalTo(currentTimeLabel)
                make.right.equalTo(mainMaskView).offset(-10)
                make.width.equalTo(40)
            }
            
            timeSlider.snp.remakeConstraints { (make) in
                make.centerY.equalTo(currentTimeLabel)
                make.left.equalTo(currentTimeLabel.snp.right).offset(10).priority(750)
                make.right.equalTo(totalTimeLabel.snp.left).offset(-10)
                make.height.equalTo(30)
            }
            
            progressView.snp.remakeConstraints { (make) in
                make.centerY.left.right.equalTo(timeSlider)
                make.height.equalTo(2)
            }
            
            playButton.snp.remakeConstraints { (make) in
                make.width.equalTo(50)
                make.height.equalTo(50)
                make.left.bottom.equalTo(bottomMaskView)
            }
            
            fullscreenButton.snp.remakeConstraints { (make) in
                make.width.equalTo(50)
                make.height.equalTo(50)
                make.centerY.equalTo(playButton)
                make.right.equalTo(bottomMaskView.snp.right)
            }
            
            self.switchBtn.snp.makeConstraints { (make) in
                make.centerY.equalTo(fullscreenButton)
                make.right.equalTo(fullscreenButton.snp.left).offset(0)
                make.width.equalTo(30)
                make.height.equalTo(20)
            }
            
            sendBarrageBtn.snp.remakeConstraints({ (make) in
                make.left.equalTo(playButton.snp.right)
                make.right.equalTo(switchBtn.snp.left).offset(-10)
                make.bottom.equalTo(bottomMaskView).offset(-10)
                make.height.equalTo(30)
            })
            
        }else{
            self.sendBarrageBtn.removeFromSuperview()
            self.backgroundView.removeFromSuperview()
            self.switchBtn.removeFromSuperview()
            bottomMaskView.snp.remakeConstraints({ (make) in
                make.bottom.left.right.equalTo(mainMaskView)
                make.height.equalTo(50)
            })
            
            playButton.snp.remakeConstraints { (make) in
                make.width.equalTo(50)
                make.height.equalTo(50)
                make.left.bottom.equalTo(bottomMaskView)
            }
            
            currentTimeLabel.snp.remakeConstraints { (make) in
                make.left.equalTo(playButton.snp.right)
                make.centerY.equalTo(playButton)
                make.width.equalTo(40)
            }
            
            timeSlider.snp.remakeConstraints { (make) in
                make.centerY.equalTo(currentTimeLabel)
                make.left.equalTo(currentTimeLabel.snp.right).offset(10).priority(750)
                make.height.equalTo(30)
            }
            
            progressView.snp.remakeConstraints { (make) in
                make.centerY.left.right.equalTo(timeSlider)
                make.height.equalTo(2)
            }
            
            totalTimeLabel.snp.remakeConstraints { (make) in
                make.centerY.equalTo(currentTimeLabel)
                make.left.equalTo(timeSlider.snp.right).offset(5)
                make.width.equalTo(40)
            }
            
            fullscreenButton.snp.remakeConstraints { (make) in
                make.width.equalTo(50)
                make.height.equalTo(50)
                make.centerY.equalTo(currentTimeLabel)
                make.left.equalTo(totalTimeLabel.snp.right)
                make.right.equalTo(bottomMaskView.snp.right)
            }
        }
        self.layoutIfNeeded()
    }
}
